package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo save(@RequestBody Cart cartParam) {
        //要将添加的这条购物信息在数据库中记录返回
        Cart cart = cartService.save(cartParam);
        return ResultInfo.success(cart);
    }

    //查询购物车
    @GetMapping("/cart/list")
    public ResultInfo findList() {
        List<Cart> cartsList = cartService.findList();
        return ResultInfo.success(cartsList);
    }


    //修改购物车
    @PostMapping("/cart/sub")
    public ResultInfo updateCart(@RequestBody Map<String, String> map) {
        String dishId = map.get("dishId");
        String setmealId = map.get("setmealId");
        Cart cartList = cartService.updateCart(dishId, setmealId);
        return ResultInfo.success(cartList);
    }



    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean() {
        cartService.clean();
        return ResultInfo.success(null);
    }
}
